{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DesignerSettings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, DesignerPrefs;

type
  TfrmDesignerSettings = class(TForm)
    TreeView: TTreeView;
    btApply: TButton;
    btCancel: TButton;
    btSave: TButton;
    Label21: TLabel;
    lbSupport: TLabel;
    pnHeading: TPanel;
    pbHeader: TPaintBox;
    Notebook: TNotebook;
    pnGeneralControls: TPanel;
    gbToolbars: TGroupBox;
    cbMainToolbar: TCheckBox;
    cbPreviewToolbar: TCheckBox;
    gbFormSettings: TGroupBox;
    cbSaveFormState: TCheckBox;
    pnTheme: TPanel;
    gbThemeSettings: TGroupBox;
    cbAutoLoadTheme: TCheckBox;
    pnDisplayControls: TPanel;
    gbPreviewSettings: TGroupBox;
    cbPreventStretch: TCheckBox;
    gbScreenSaver: TGroupBox;
    cbWindowsSaver: TCheckBox;
    cbMediateSaver: TCheckBox;
    rbVertical: TRadioButton;
    rbHorizontal: TRadioButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure TreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure lbSupportClick(Sender: TObject);
    procedure pbHeaderPaint(Sender: TObject);
  private
    { Private declarations }
    FManager: TAHMDesignerPrefsManager;
    FUpdated: Boolean;
    FHeader: TBitmap;
  protected
    { Protected declarations }
    procedure PopulateSettings;
    procedure SaveSettings;
    procedure InitialiseControls;
    procedure UpdateGUIState;
  public
    { Public declarations }
    property Manager: TAHMDesignerPrefsManager read FManager write FManager;
    function ConfigureSettings: Boolean;
  end;


implementation

{$R *.dfm}

uses ApplicationManager, Utilities, ColourRenderer, Colour;

//-----------------------------------------------------------------------------
//------------------------------ Form Events ----------------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerSettings.FormCreate(Sender: TObject);
begin
  // Create buffer bitmap to render header gradient
  FHeader := TBitmap.Create;
end;

procedure TfrmDesignerSettings.FormDestroy(Sender: TObject);
begin
  // Free header gradient buffer
  FHeader.Free;
end;

//-----------------------------------------------------------------------------
//---------------------------- Button Events ----------------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerSettings.btSaveClick(Sender: TObject);
begin
  // Store modified settings in preferences manager
  SaveSettings;
end;

//-----------------------------------------------------------------------------
//------------------------- Miscellaneous Events ------------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerSettings.TreeViewChange(Sender: TObject; Node: TTreeNode);
begin
  // Show relevant notebook page for selected tree node
  if Assigned(TreeView.Selected) and (TreeView.Selected.SelectedIndex >= 0) then
    Notebook.PageIndex := TreeView.Selected.SelectedIndex;

  // Update header panel
  UpdateGUIState;
end;

procedure TfrmDesignerSettings.lbSupportClick(Sender: TObject);
begin
  DisplayBrowserSupport;
end;

procedure TfrmDesignerSettings.pbHeaderPaint(Sender: TObject);
begin
  with Sender as TPaintBox do
  begin
    // Rebuild gradient if dimensions change, or on first render
    if (FHeader.Width <> Width) or (FHeader.Height <> Height) then
    begin
      FHeader.Width := Width;
      FHeader.Height := Height;
      with ColourRendererInstance do
        BlendBitmap(TAHMCanvasColour.GradientLo, TAHMCanvasColour.GradientHi,
                    TAHMCanvasColour.GradientLo, TAHMCanvasColour.GradientHi,
                    FHeader);
    end;

    // Render gradient
    Canvas.Draw(0, 0, FHeader);

    // Render caption
    Canvas.Brush.Style := bsClear;
    Canvas.TextOut(12, 9, Notebook.ActivePage);
  end;
end;

//-----------------------------------------------------------------------------
//--------------------- User Interface Configuration --------------------------
//-----------------------------------------------------------------------------

function TfrmDesignerSettings.ConfigureSettings: Boolean;
begin
  // Populate controls from current application settings
  PopulateSettings;

  // Initialise display of form controls
  InitialiseControls;

  // Now show form modally
  ShowModal;
  Result := FUpdated;
end;

procedure TfrmDesignerSettings.PopulateSettings;
begin
  // Populate settings from preferences manager
  with FManager do
  begin
    // Populate form configuration
    cbSaveFormState.Checked := SaveFormPosition;

    // Populate screen saver configuration
    cbWindowsSaver.Checked := WindowsScreenSaver;
    cbMediateSaver.Checked := MediateScreenSaver;

    // Populate toolbar configuration
    cbMainToolbar.Checked := LargeMainToolbar;
    cbPreviewToolbar.Checked := LargePreviewToolbar;

    // Populate theme configuration
    cbAutoLoadTheme.Checked := AutoOpenThemeName <> '';

    // Populate preview configuration
    cbPreventStretch.Checked := PreventPreviewStretch;

    // Populate layout configuration
    if HorizontalLayout then rbHorizontal.Checked := True
                        else rbVertical.Checked := True;
  end;
end;

procedure TfrmDesignerSettings.SaveSettings;
begin
  FUpdated := True;

  // Update settings in preferences manager
  with FManager do
  begin
    // Update form configuration
    SaveFormPosition := cbSaveFormState.Checked;

    // Update screen saver configuration
    WindowsScreenSaver := cbWindowsSaver.Checked;
    MediateScreenSaver := cbMediateSaver.Checked;

    // Update toolbar configuration
    LargeMainToolbar := cbMainToolbar.Checked;
    LargePreviewToolbar := cbPreviewToolbar.Checked;

    // Update theme configuration
    if cbAutoLoadTheme.Checked then
      if ApplicationManagerInstance.ThemeManager.ThemeName <> '' then
        AutoOpenThemeName := ApplicationManagerInstance.ThemeManager.ThemeName
      else
        AutoOpenThemeName := ApplicationManagerInstance.PreferencesManager.ThemeName
    else
      AutoOpenThemeName := '';

    // Update preview configuration
    PreventPreviewStretch := cbPreventStretch.Checked;

    // Update layout configuration
    HorizontalLayout := rbHorizontal.Checked;
  end;
end;

procedure TfrmDesignerSettings.InitialiseControls;
begin
  // Ensure first page of notebook is selected
  Notebook.PageIndex := 0;

  // Expand treeview
  TreeView.FullExpand;

  // Start GUI in a consistent state
  UpdateGUIState;
end;

procedure TfrmDesignerSettings.UpdateGUIState;
begin
  // Configure panel caption
  pbHeader.Invalidate;
end;

end.
